package com.plm.mr.mapjoin;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * Map Join
 * MapJoin 适用于关联表中有小表的情形
 */
public class MapJoinDriver {
    public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException, ClassNotFoundException {
        args = new String[]{"E:/projects/hadoop/fs-input/inputtable/pd.txt", "E:\\projects\\hadoop\\fs-input\\inputtable2", "E:\\projects\\hadoop\\fs-output\\output4"};

        // 1获取 job 信息
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        // 2设置加载 jar 包路径
        job.setJarByClass(MapJoinDriver.class);
        // 3关联 mapper
        job.setMapperClass(MapJoinMapper.class);
        // 4设置 Map 输出 KV 类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);
        // 5设置最终输出 KV 类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        // 加载缓存数据
        // 缓存普通文件到 Task 运行节点: new URI("file:///e:/cache/pd.txt")
        // /如果是集群运行,需要设置 HDFS 路径: new URI("hdfs://hadoop102:8020/cache/pd.txt")
        job.addCacheFile(new URI("file:///" + args[0]));
        // Map 端 Join 的逻辑不需要 Reduce 阶段，设置 reduceTask 数量为 0
        job.setNumReduceTasks(0);

        // 6设置输入输出路径
        FileInputFormat.setInputPaths(job, new Path(args[1]));
        FileOutputFormat.setOutputPath(job, new Path(args[2]));

        // 7提交
        boolean b = job.waitForCompletion(true);
        System.exit(b ? 0 : 1);

    }
}
